<template>
	<view class="callBackPage">
		<view class="topContent"></view>
		<view class="callBackForm">
			<view class="formItem">
				<u--input placeholder="请输入问题标题" border="surround" v-model="title" :maxlength="50"></u--input>
			</view>
			<view class="formItem">
				<u--textarea v-model="details" placeholder="请输入问题详情" :maxlength="500" count :autoHeight="true">
				</u--textarea>
			</view>
			<view class="formItem uploadContent">
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="9">
					<view class="uploadIcon">
						<u-icon name="plus" size="50rpx" color="#D0D0D0FF"></u-icon>
					</view>
				</u-upload>
			</view>
		</view>
		<view class="callbackBtn" @click="doCommit">提交问题反馈</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: "",
				details: "",
				fileList1: [],
				userId: "",
				api: this.$api.baseUrl,
			}
		},
		onLoad() {
			uni.getStorage({
				key: "userInfo",
				success: (userInfo) => {
					this.userId = userInfo.data.id;
				}
			})

		},
		methods: {
			async afterRead(event) {
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: this.$appFunc.getFileUrl(result)
						// url: this.api + '/' + result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					this.$request.upload(this.$api.uploadImage, url).then(res => {
						resolve(JSON.parse(res).message)
						uni.hideLoading()
					});
				})
			},
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			doCommit() {
				if (!this.title) {
					uni.showToast({
						icon: "none",
						title: "请输入问题标题"
					})
					return;
				}
				if (!this.details) {
					uni.showToast({
						icon: "none",
						title: "请输入问题描述"
					})
					return;
				}
				let imageStr = "";
				this.fileList1.map(item => {
					if (imageStr) {
						imageStr = imageStr + "," + item.url.replaceAll(this.api + '/', "")
					} else {
						imageStr = item.url.replaceAll(this.api + '/', "")
					}
				})
				this.$request.post(this.$api.doReport, {
					createBy: this.userId,
					titleName: this.title,
					details: this.details,
					image: imageStr
				}).then(res => {
					uni.hideLoading()
					uni.navigateBack({})
					setTimeout(() => {
						uni.showToast({
							icon: "success",
							title: "反馈成功"
						})
					},300)
				})
			}
		}
	}
</script>

<style lang="scss">
	.callBackPage {
		background-color: #fff;
		min-height: 100vh;
		padding-top: 40rpx;

		.topContent {
			width: 560rpx;
			height: 168rpx;
			background-color: #D4EBFF;
			margin: 0rpx auto;
		}

		.callBackForm {
			padding: 34rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-around;

			.formItem {
				margin-bottom: 20rpx;

				.u-input {
					background: #F6F7FB;
					border: none
				}

				.u-textarea {
					min-height: 300rpx;
					background: #F6F7FB;
					border: none
				}

				textarea {
					min-height: 300rpx;
				}
			}
		}

		.uploadContent {
			.u-upload__wrap {
				display: flex;
				justify-content: flex-start;
			}

			.uploadIcon {
				background-color: #F6F7FB;
				width: 140rpx;
				height: 140rpx;
				font-size: 30rpx;
				margin: 20rpx 10rpx 0 0;
				border: none;
				display: flex;
				justify-content: center;
				border-radius: 8rpx;
			}

			.u-upload__wrap__preview {
				margin-top: 20rpx;
			}
		}

		.callbackBtn {
			background: linear-gradient(180deg, #87C5FC 0%, #247FFF 100%);
			border-radius: 48rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: bold;
			color: #FFFFFF;
			font-size: 34rpx;
			height: 96rpx;
			width: 680rpx;
			text-align: center;
			line-height: 96rpx;
			margin: 0 auto;
		}
	}
</style>
